package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.ToolBoxModles;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/5.
 */
public class ToolBoxAdapter extends RecyclerView.Adapter<ToolBoxAdapter.ToolViewHolder> {
    private Context context;
    private List<ToolBoxModles> modlesList;
    private int count;
    private List<Boolean> isShow = new ArrayList<>();
    private ToolInNameAdapter adapter;

    public ToolBoxAdapter(Context context, List<ToolBoxModles> modlesList) {
        this.context = context;
        this.modlesList = modlesList;
        for (int i = 0; i < 3; i++) {
            this.count += modlesList.get(i).getDestinations().size();
        }
    }

    @Override
    public ToolViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_tool_overseas, null);
        ToolViewHolder holder = new ToolViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ToolViewHolder holder, final int position) {
        isShow.add(position, true);
        if (position < 13) {
            holder.tvItemToolAreaname.setText(modlesList.get(0).getDestinations().get(position).getName_zh_cn());
            final RecyclerView recyclerView = new RecyclerView(context);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isShow.get(position)) {
                        holder.linItemToolRecycler.removeView(recyclerView);
                        holder.ivItemToolDown.setBackgroundResource(R.mipmap.icon_destination_brief_title_arrows_up);
                        RecyclerView recyclerView = new RecyclerView(context);
                        adapter = new ToolInNameAdapter(context, modlesList.get(0).getDestinations().get(position));
                        recyclerView.setAdapter(adapter);

                        LinearLayoutManager linManager = new LinearLayoutManager(context);
                        recyclerView.setLayoutManager(linManager);
                        holder.linItemToolRecycler.setVisibility(View.VISIBLE);
                        holder.linItemToolRecycler.addView(recyclerView);
                        isShow.set(position, false);
                    } else {
                        holder.ivItemToolDown.setBackgroundResource(R.mipmap.icon_destination_brief_title_arrows_down);
                        holder.linItemToolRecycler.setVisibility(View.GONE);
                        holder.linItemToolRecycler.removeView(recyclerView);
                        isShow.set(position, true);
                    }
                }
            });
        }
        if (position >= 13 && position < 28) {
            int i = position - 13;
            holder.tvItemToolAreaname.setText(modlesList.get(1).getDestinations().get(i).getName_zh_cn());
        }
        if (position >= 28) {
            int i = position - 28;
            holder.tvItemToolAreaname.setText(modlesList.get(2).getDestinations().get(i).getName_zh_cn());
        }


    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class ToolViewHolder extends RecyclerView.ViewHolder {
        private TextView tvItemToolAreaname;
        private ImageView ivItemToolDown;
        private LinearLayout linItemToolRecycler;

        public ToolViewHolder(View itemView) {
            super(itemView);
            tvItemToolAreaname = (TextView) itemView.findViewById(R.id.tv_item_tool_areaname);
            ivItemToolDown = (ImageView) itemView.findViewById(R.id.iv_item_tool_down);
            linItemToolRecycler = (LinearLayout) itemView.findViewById(R.id.lin_item_tool_recycler);
        }
    }
}
